package xhwl.hmo.mall.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.CommentListAdapter;
import xhwl.hmo.mall.http.api.ProductsCommentApi;
import xhwl.hmo.mall.http.bean.resp.CommentListResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.pulltorefresh.PullToRefreshRecyclerView;
import xhwl.pulltorefresh.callback.PullToRefreshListener;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * 评价列表
 */
public class CommentListActivity extends BaseActivity implements PullToRefreshListener {
    private PullToRefreshRecyclerView mRecyclerView;
    CommentListAdapter adapter;
    List<CommentListResp.COMMENTLISTBean> mList = new ArrayList<>();
    private int mPage = 1;
    private String good_id;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        good_id =getIntent().getStringExtra("good_id");
        setContentView(R.layout.activity_comment_list);
    }

    @Override
    public void initViews() {
        initTitleViews();
        mRecyclerView = (PullToRefreshRecyclerView) findViewById(R.id.mRecyclerView);
        initRecylerView();
        adapter = new CommentListAdapter(this, mList);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText("评价页");
    }

    @Override
    public void initListener() {
      mRecyclerView.setPullToRefreshListener(this);
    }

    @Override
    public void loadData() {
        getCommentList(1, good_id);
    }

    private void getCommentList(final int page_num, String goods_id) {
        ProductsCommentApi api = new ProductsCommentApi(page_num, goods_id);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp<CommentListResp>>() {
            @Override
            public void onNext(HttpResp<CommentListResp> resp) {
                if (page_num == 1) {
                    if (resp != null) {
                        mRecyclerView.setRefreshComplete();
                        mPage = 1;
                        mList.clear();
                        if (resp != null && resp.data.getCOMMENT_LIST().size() > 0) {
                            mList.addAll(resp.data.getCOMMENT_LIST());
                        }
                        adapter.notifyDataSetChanged();
                    } else {
                        mRecyclerView.setRefreshFail();
                    }
                } else {
                    if (resp != null) {
                        mRecyclerView.setLoadMoreComplete();
                        if (resp.data.getCOMMENT_LIST().size() > 0) {
                            mPage = page_num;
                            mList.addAll(resp.data.getCOMMENT_LIST());
                            adapter.notifyDataSetChanged();
                        } else {
                            mRecyclerView.setLoadMoreFail();
                        }
                    }
                }
            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                if (page_num == 1) {
                    mRecyclerView.setRefreshFail();
                } else {
                    mRecyclerView.setLoadMoreFail();
                }
            }
        });

    }

    private void initRecylerView() {
        //设置EmptyView
        View emptyView = View.inflate(this, R.layout.layout_empty_view, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mRecyclerView.setEmptyView(emptyView);

        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView mRecyclerView, int dx, int dy) {
                super.onScrolled(mRecyclerView, dx, dy);


            }
        });
        //设置是否开启上拉加载
        mRecyclerView.setLoadingMoreEnabled(true);
        //设置是否开启下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        //设置是否显示上次刷新的时间
        mRecyclerView.displayLastRefreshTime(false);
    }

    @Override
    public void onRefresh() {
        getCommentList(1, good_id);
    }

    @Override
    public void onLoadMore() {
        getCommentList(mPage+1, good_id);
    }
}
